
namespace Pixels.Core
{
    using System;
    using System.Collections.Generic;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Pixels.Exceptions;
    using Pixels.ListItems;
    using Pixels.ListItems.DataLayer;

    internal class ListItemAdapter
    {
        #region private variables
        private DataAccessLayer dataLayer;
        private static object synclock = new object();
        private static ListItemAdapter _listitemadapter = null;
        #endregion

        #region Constructors
        private ListItemAdapter()
        {
            dataLayer = new DataAccessLayer();
        }
        #endregion

        #region Methods

        public static ListItemAdapter GetInstance()
        {
            lock (synclock) // thread safe
            {
                if (_listitemadapter == null)
                {
                    _listitemadapter = new ListItemAdapter();
                }
            }
            return _listitemadapter;
        }

        public void ListItemAddEdit(IListItem objListItem, bool updateifExists)
        {
            try
            {
                dataLayer.ListItemAddEdit(objListItem, updateifExists);
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("ListItem could not be added/modified due to invalid data/technical issues.");
            }
        }

        public void ListItemDelete(Guid ListItemID)
        {
            try
            {
                if (!ListItemID.IsNull())
                    dataLayer.ListItemDelete(ListItemID);
                else
                    new Exception("ListItemID can't be null or Empty");
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("ListItem could not be deleted/Invalid ListItemID.");
            }
        }

        public IListItem ListItemFetch(Guid ListItemID)
        {
            try
            {
                if (!ListItemID.IsNull())
                {
                    IListItem objListItem = dataLayer.ListItemFetch(ListItemID);
                    return objListItem;
                }
                else
                {
                    new Exception("ListItemID can't be null or Empty");
                    return null;
                }
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("Error in Fetching ListItem Detail.");
            }
        }

        public IListItem ListItemFetchByExtentedListItemID(Guid ExtentedListItemID)
        {
            try
            {
                if (!ExtentedListItemID.IsNull())
                {
                    IListItem objListItem = dataLayer.ListItemFetch(ExtentedListItemID);
                    return objListItem;
                }
                else
                {
                    new Exception("ExtentedListItemID can't be null or Empty");
                    return null;
                }
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("Error in Fetching ListItem Detail.");
            }
        }

        public IList<IListItem> ListItemsFetch()
        {
            try
            {
                IList<IListItem> ListItems = dataLayer.ListItemsFetch();
                return ListItems;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("Error in Fetching ListItem Detail.");
            }
        }

        public IList<IListItem> ListItemsFetchQuery(string Query)
        {
            try
            {
                IList<IListItem> ListItems = dataLayer.ListItemFetchByQuery(Query);
                return ListItems;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("Error in Fetching ListItem Detail.");
            }
        }   
        #endregion
    }
}
